#!/bin/bash
#
#  Simple script to automate switching your Konsole schema when running apps.
#  Version 0.9
#
#  Copyright (C) 2007 Michael T. Dean
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#  First created: 2/23/07
#
##################################################################################

getKonsoleInfo()
{
	# Make sure we're in Konsole
	if [ ! -z "${KONSOLE_DCOP}" ]
	then
		inKonsole='1'
		konsole=`echo $KONSOLE_DCOP | cut -d\( -f 2 | cut -d, -f1`
		session=`dcop $konsole konsole currentSession`
		origSchema=`dcop $konsole $session schema`
	else
		inKonsole='0'
	fi
}

getAppInfo()
{
	appName="${1}"

	appInfo=`cat ~/.konsole_dcop/schemas | grep -w --color=never ${appName}`
	appSchema=`echo "${appInfo}" | awk '{ print $2 }'`
	appPath=`echo "${appInfo}" | awk '{ print $3 }'`
}

getKonsoleInfo
getAppInfo ${*}
shift

if [ "${inKonsole}" == "1" ]
then
	dcop $konsole $session setSchema ${appSchema}
	${appPath} ${*}
	dcop $konsole $session setSchema "vim.schema"		# Workaround for Konsole bug
	dcop $konsole $session setSchema "${origSchema}"
else
	${appPath} "${*}"
fi

